package com.hzcx.app.simplechat.ui.moment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.common.Common;
import com.hzcx.app.simplechat.ui.moment.adapter.NewMessageAdapter;
import com.hzcx.app.simplechat.ui.moment.bean.NewMessageListBean;
import com.hzcx.app.simplechat.ui.moment.contract.NewMessageContract;
import com.hzcx.app.simplechat.ui.moment.presenter.NewMessagePresenter;
import com.hzcx.app.simplechat.ui.user.UserInfoActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 新消息列表
 */
public class NewMessageActivity extends BaseActivity<NewMessagePresenter> implements NewMessageContract.View, OnRefreshListener,
        BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.rv_new_msg)
    RecyclerView rvNewMsg;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smartRefresh;
    @BindView(R.id.ll_null)
    LinearLayout llNull;

    private boolean isRefresh = true;
    private int page = 1;
    private List<NewMessageListBean> messageData;
    private NewMessageAdapter messageAdapter;

    @Override
    public int getLayout() {
        return R.layout.activity_new_message;
    }

    @Override
    public void initPresenter() {
        mPresenter = new NewMessagePresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("我的消息");
        tvTitleRight.setText("清空");
        tvTitleRight.setTextColor(getResources().getColor(R.color.color_575B61));

        messageData = new ArrayList<>();
        messageAdapter = new NewMessageAdapter(messageData);
        rvNewMsg.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvNewMsg.setAdapter(messageAdapter);

        smartRefresh.setOnRefreshListener(this);
        messageAdapter.setOnLoadMoreListener(this);
        messageAdapter.setOnItemChildClickListener(this);
        messageAdapter.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        smartRefresh.autoRefresh();
    }

    @OnClick({R.id.tv_title_right})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_title_right:
                clearMsg();
                break;
        }
    }

    /**
     * 清空消息
     */
    private void clearMsg() {
        if (messageData == null || messageData.size() <= 0) {
            showError("暂时没有新消息可以清空");
            return;
        }
        showConfirmDialog("提示", "确认清空吗？清空后不可恢复", null, () ->
                mPresenter.clearNewMessageList(NewMessageActivity.this));
    }

    /**
     * 新消息获取成功
     *
     * @param data
     */
    @Override
    public void messageResult(List<NewMessageListBean> data) {
        smartRefresh.finishRefresh();
        messageAdapter.loadMoreComplete();
        try {
            if (isRefresh) {
                messageData.clear();
            }
            messageData.addAll(data);
            if (data.size() < Common.PAGE_COUNT) {
                messageAdapter.loadMoreEnd();
            }
            messageAdapter.notifyDataSetChanged();
            if (messageData == null || messageData.size() <= 0) {
                llNull.setVisibility(View.VISIBLE);
            } else {
                llNull.setVisibility(View.GONE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加载失败
     */
    @Override
    public void onFail() {
        smartRefresh.finishRefresh();
        messageAdapter.loadMoreFail();
    }

    /**
     * 消息列表清空成功
     */
    @Override
    public void clearSuccess() {
        showError("清空成功");
        messageData.clear();
        messageAdapter.notifyDataSetChanged();
        llNull.setVisibility(View.VISIBLE);
    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMoreRequested() {
        isRefresh = false;
        page++;
        mPresenter.getNewMessageList(this, page);
    }

    /**
     * 刷新
     *
     * @param refreshLayout
     */
    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isRefresh = true;
        page = 1;
        mPresenter.getNewMessageList(this, page);
    }

    /**
     * 子控件点击
     *
     * @param adapter
     * @param view
     * @param position
     */
    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.iv_head: //点击头像，到个人信息页面
                startActivity(new Intent(this, UserInfoActivity.class)
                        .putExtra(UserInfoActivity.INTENT_USER_ID, messageData.get(position).getMember_id()));
                break;
        }
    }

    /**
     * 列表点击
     *
     * @param adapter
     * @param view
     * @param position
     */
    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        startActivity(new Intent(this, MomentInfoActivity.class)
                .putExtra(MomentInfoActivity.INTENT_MOMENT_ID, messageData.get(position).getDynamic_id()));
    }

}
